var _ag=null;
var _skin=null;
var _videofile=null;
var _previewfile=null;
var _titletext=null;

function createPlayer(hostdiv, videofile, previewfile, titletext)
{  
_videofile = videofile;
_previewfile = previewfile;
_titletext = titletext;

Sys.Silverlight.createObject("/player.xaml", document.getElementById(hostdiv), "ag",
                         {width:'333', height:'334', inplaceInstallPrompt:true, background:'white', version:'0.9'},
                         {onError:'onErrorHandler', onLoad:onLoaded},
                         null);                         
}

function download(file, cb) {
var dl = _ag.createObject("downloader");
dl.addEventListener("completed", cb);
dl.open("get", file, true);
dl.send();
}

function onLoaded(sender, context, root) {
_ag = sender;
_ag.content.onFullScreenChange = "onFullScreenChanged";

download("/Fonts/Fonts.zip", onFontDownload);
}

function onErrorHandler(sender, args) 
{        
}

function onFontDownload(sender) 
{
if (!_skin) {
  _skin = new videoSkin($f("player"));
}

_skin.playVideo(_videofile, _previewfile, _titletext);
}

function $f(name) {
return _ag.content.findName(name);
}

function $(name) {
return document.getElementById(name);
}

function onStopVideo(sender) {
if (_skin) {
  _skin.stopVideo();
}
}

function onFullScreenChanged(sender, args) {
var host = sender.getHost();

var fullScreenWidth = host.content.actualWidth;
var fullScreenHeight = host.content.actualHeight;

if (host.content.fullScreen) {
  sender.findName("media").play();
  var fsm = sender.findName("fsm");
  fsm.width = fullScreenWidth;
  fsm.height = fullScreenHeight;
  fsm.visibility = "Visible";
}
else {
  sender.findName("fsm").visibility = "Hidden";
}
}

function goFSM(sender, args) {
sender.getHost().content.fullScreen = true;
}